/**
 * @deprecated
 * Test code used by test-01.htm
 */
var vvg = {
   id: {
      MAP_DIV: 'map_element'  // default name, reassignable on init
      ,LAYER_SWITCHER: 'map_layer_switcher'
      ,NAV_CONTROL: 'map_nav_control'
      ,VECTOR_LAYER: 'vector_layer'
      ,VECTOR_LAYER_TOOLBAR: 'vector_layer_toolbar'}
   ,features: []
};

vvg.handle_featureadded = function(event) {
   var newFeature = event.feature;
   vvg.vector_layer = newFeature.layer;

   // clear existing features: but keep references for history
   vvg.vector_layer.removeFeatures(vvg.features);
   vvg.features[vvg.features.length] = newFeature;

   // log new feature's geometry
   console.log('\nnew feature: ');
   var vertices = newFeature.geometry.getVertices();
   for(var j = 0; j < vertices.length; j++) {
      console.log(' vertex ' + j );
      console.log('  x: ' + vertices[j].x);
      console.log('  y: ' + vertices[j].y);
   }
   
   //for(var i = 0; i < ll.features.length; i++) { ll.features[i].geometry.components }
   //for(var i = 0; i < ll.features.length; i++) { ll.features[i].geometry.getVertices() }
}

vvg.handle_visibilitychanged_vectorLayer = function(event) {
   var toolbar = vvg.map.getControl(vvg.id.VECTOR_LAYER_TOOLBAR);
   if(event.object.visibility === true) {
      toolbar.activate();
   } else {
      toolbar.deactivate();
      toolbar.defaultControl.activate();
   }
}

vvg.handle_allEvents = function(event) {
   console.log(event.type);
}

vvg.init = function(map_div) {
   vvg.id.MAP_DIV = map_div;
   
   var layers = [
      new OpenLayers.Layer.Google(
         "Google Streets", // the default
         {numZoomLevels: 20})
      ,new OpenLayers.Layer.Google(
         "Google Physical",
         {type: google.maps.MapTypeId.TERRAIN})
      ,new OpenLayers.Layer.Google(
         "Google Hybrid",
         {type: google.maps.MapTypeId.HYBRID, numZoomLevels: 20})
      ,new OpenLayers.Layer.Google(
         "Google Satellite",
         {type: google.maps.MapTypeId.SATELLITE, numZoomLevels: 22})
      ,new OpenLayers.Layer.Vector(
         'Line'
         ,{
            eventListeners: {
               visibilitychanged: vvg.handle_visibilitychanged_vectorLayer}
         })
   ];
   for (var i=0; i<layers.length; ++i) {
         layers[i].animationEnabled = true;
         layers[i].sphericalMercator = true;
         layers[i].transitionEffect = "resize";
   }

   var map = new OpenLayers.Map(vvg.id.MAP_DIV, {
      projection: new OpenLayers.Projection('EPSG:900913')
//               ,displayProjection: new OpenLayers.Projection('EPSG:4326')
      ,layers: layers
      ,controls: [
         new OpenLayers.Control.LayerSwitcher({id: vvg.id.LAYER_SWITCHER})
         ,new OpenLayers.Control.MousePosition()
         ,new OpenLayers.Control.EditingToolbar(
            // TODO: get vector layer with less fragile method
            layers[4]
            ,{id: vvg.id.VECTOR_LAYER_TOOLBAR})
         ]
   });

   // Google.v3 uses EPSG:900913 as projection, so we have to
   // transform our coordinates
   map.setCenter(
      new OpenLayers.LonLat(10.2, 48.9).transform(
         new OpenLayers.Projection("EPSG:4326"),
         map.getProjectionObject())
      ,5);

   vvg.map = map;

   // TODO: get line drawing control with less fragile method
   vvg.linectrl = map.controls[5];
   vvg.linectrl.events.register('featureadded', vvg.linectrl, vvg.handle_featureadded);
}

// work-around for blank map problem when browser window is resized
vvg.redrawMap = function() {
   var map = vvg.map;

   // probably redundant
   map.render(vvg.id.MAP_DIV);

   // required
   for (var i = 0; i < map.layers.length; i++) {
      if(map.layers[i].getVisibility() === true)
         map.layers[i].redraw();
   }
}